package com.arpa.wms.controller;

import cn.hutool.core.util.StrUtil;
import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.wms.domain.dto.GoodsUnitConversionDTO;
import com.arpa.wms.domain.entity.GoodsUnitConversion;
import com.arpa.wms.domain.vo.GoodsUnitConversionVO;
import com.arpa.wms.service.IGoodsUnitConversionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * <p>
 * 商品单位换算 Controller
 * </p>
 *
 * @author LEO
 * @since 2020-08-03
 */
@Api(tags = "商品单位换算")
@RestController
@RequestMapping(value="/goodsUnitConversion", produces = "application/json;charset=UTF-8")
@Log4j2
public class GoodsUnitConversionController {

   @Autowired
   private IGoodsUnitConversionService  iGoodsUnitConversionService;

    /**
     * 获取 商品单位换算 列表
     *
     * @param goodsUnitConversionDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 商品单位换算 列表", notes = "获取 商品单位换算 列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = GoodsUnitConversionVO.class))
    @NeedLogin
    @GetMapping("/list")
    public Result list(GoodsUnitConversionDTO goodsUnitConversionDTO,HttpServletRequest request) {
        try {
            GoodsUnitConversionVO goodsUnitConversionVoSum= iGoodsUnitConversionService.queryListSum(goodsUnitConversionDTO);
            List<GoodsUnitConversionVO> goodsUnitConversionVOList = iGoodsUnitConversionService.queryList(goodsUnitConversionDTO);
            goodsUnitConversionVoSum.setGroupCode(UserUtil.getBranchCode());
            Page returnPage = new Page(goodsUnitConversionDTO.getPageNum(),goodsUnitConversionDTO.getPageSize());
            returnPage.setRecords(goodsUnitConversionVOList);
            returnPage.setTotal(goodsUnitConversionVoSum.getTotal());
            returnPage.setSumObject(goodsUnitConversionVoSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 商品单位换算
     *
     * @param entity GoodsUnitConversion
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 商品单位换算 ", notes = "添加 商品单位换算 ")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping("/save")
    public Result create(GoodsUnitConversion entity, HttpServletRequest request) {
        try {
            entity.setGroupCode(UserUtil.getBranchCode());
            iGoodsUnitConversionService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
    * 获取商品单位换算详情
    *
    * @param code
    * @param request
    * @return
    */
    @ApiOperation(value = "获取商品单位换算详情", notes = "获取商品单位换算详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "code", value = "商品单位换算code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = GoodsUnitConversion.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result goodsUnitConversion(@PathVariable String code, HttpServletRequest request) {
        try {
            GoodsUnitConversion entity = iGoodsUnitConversionService.getOne(new QueryWrapper<GoodsUnitConversion>().lambda().eq(GoodsUnitConversion::getCode,code).eq(GoodsUnitConversion::getGroupCode,UserUtil.getBranchCode()));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改商品单位换算
     *
     * @param entity 商品单位换算
     * @param request
     * @return
     */
    @ApiOperation(value = "修改商品单位换算", notes = "修改商品单位换算")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping("/update")
    public Result update(GoodsUnitConversion entity,HttpServletRequest request) {
        if(StrUtil.isBlank(entity.getCode())){
            return Result.error("code不能为空");
        }
        int updateNum = iGoodsUnitConversionService.updateByCode(entity);
        if(updateNum == 0){
            return Result.error("没有更新数据！code:"+entity.getCode());
        }else{
            return Result.ok("修改成功,更新"+updateNum+"条数据！");
        }
    }

    /**
     * 删除 商品单位换算
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 商品单位换算 ", notes = "删除 商品单位换算 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "/batchdel")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iGoodsUnitConversionService.remove(new QueryWrapper<GoodsUnitConversion>().lambda().in(GoodsUnitConversion::getCode,codes).eq(GoodsUnitConversion::getGroupCode,UserUtil.getBranchCode()));
            return Result.ok();
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

 }
